package com.zoo.diannaopeijian.mapper;

import static org.apache.ibatis.jdbc.SqlBuilder.BEGIN;
import static org.apache.ibatis.jdbc.SqlBuilder.DELETE_FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.INSERT_INTO;
import static org.apache.ibatis.jdbc.SqlBuilder.ORDER_BY;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT_DISTINCT;
import static org.apache.ibatis.jdbc.SqlBuilder.SET;
import static org.apache.ibatis.jdbc.SqlBuilder.SQL;
import static org.apache.ibatis.jdbc.SqlBuilder.UPDATE;
import static org.apache.ibatis.jdbc.SqlBuilder.VALUES;
import static org.apache.ibatis.jdbc.SqlBuilder.WHERE;

import com.zoo.diannaopeijian.domain.Stock;
import com.zoo.diannaopeijian.domain.StockExample.Criteria;
import com.zoo.diannaopeijian.domain.StockExample.Criterion;
import com.zoo.diannaopeijian.domain.StockExample;
import com.zoo.diannaopeijian.domain.Ware;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

public class StockSqlProvider {

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table stock
     *
     * @mbggenerated
     */
    public String countByExample(StockExample example) {
        BEGIN();
        SELECT("count(*)");
        FROM("stock");
        applyWhere(example, false);
        return SQL();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table stock
     *
     * @mbggenerated
     */
    public String deleteByExample(StockExample example) {
        BEGIN();
        DELETE_FROM("stock");
        applyWhere(example, false);
        return SQL();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table stock
     *
     * @mbggenerated
     */
    public String insertSelective(Stock record) {
        BEGIN();
        INSERT_INTO("stock");
        
        if (record.getStockid() != null) {
            VALUES("StockId", "#{stockid,jdbcType=INTEGER}");
        }
        
        if (record.getWareid() != null) {
            VALUES("WareId", "#{wareid,jdbcType=INTEGER}");
        }
        
        if (record.getWarehouseid() != null) {
            VALUES("WarehouseId", "#{warehouseid,jdbcType=INTEGER}");
        }
        
        if (record.getStocknum() != null) {
            VALUES("StockNum", "#{stocknum,jdbcType=INTEGER}");
        }
        
        return SQL();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table stock
     *
     * @mbggenerated
     */
    public String selectByExample(StockExample example) {
        BEGIN();
        if (example != null && example.isDistinct()) {
            SELECT_DISTINCT("StockId");
        } else {
            SELECT("StockId");
        }
        SELECT("WareId");
        SELECT("WarehouseId");
        SELECT("StockNum");
        FROM("stock");
        applyWhere(example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            ORDER_BY(example.getOrderByClause());
        }
        
        return SQL();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table stock
     *
     * @mbggenerated
     */
    public String updateByExampleSelective(Map<String, Object> parameter) {
        Stock record = (Stock) parameter.get("record");
        StockExample example = (StockExample) parameter.get("example");
        
        BEGIN();
        UPDATE("stock");
        
        if (record.getStockid() != null) {
            SET("StockId = #{record.stockid,jdbcType=INTEGER}");
        }
        
        if (record.getWareid() != null) {
            SET("WareId = #{record.wareid,jdbcType=INTEGER}");
        }
        
        if (record.getWarehouseid() != null) {
            SET("WarehouseId = #{record.warehouseid,jdbcType=INTEGER}");
        }
        
        if (record.getStocknum() != null) {
            SET("StockNum = #{record.stocknum,jdbcType=INTEGER}");
        }
        
        applyWhere(example, true);
        return SQL();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table stock
     *
     * @mbggenerated
     */
    public String updateByExample(Map<String, Object> parameter) {
        BEGIN();
        UPDATE("stock");
        
        SET("StockId = #{record.stockid,jdbcType=INTEGER}");
        SET("WareId = #{record.wareid,jdbcType=INTEGER}");
        SET("WarehouseId = #{record.warehouseid,jdbcType=INTEGER}");
        SET("StockNum = #{record.stocknum,jdbcType=INTEGER}");
        
        StockExample example = (StockExample) parameter.get("example");
        applyWhere(example, true);
        return SQL();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table stock
     *
     * @mbggenerated
     */
    public String updateByPrimaryKeySelective(Stock record) {
        BEGIN();
        UPDATE("stock");
        
        if (record.getWareid() != null) {
            SET("WareId = #{wareid,jdbcType=INTEGER}");
        }
        
        if (record.getWarehouseid() != null) {
            SET("WarehouseId = #{warehouseid,jdbcType=INTEGER}");
        }
        
        if (record.getStocknum() != null) {
            SET("StockNum = #{stocknum,jdbcType=INTEGER}");
        }
        
        WHERE("StockId = #{stockid,jdbcType=INTEGER}");
        
        return SQL();
    }

    public String getWareStock(Map<String, Object> parameter) {
        BEGIN();

        SELECT_DISTINCT("StockId");
        SELECT("WareName");
        SELECT("e.WareId");
        SELECT("ComponentName");
        SELECT("Barcode");
        SELECT("StockNum");
        SELECT("WarehouseName");
        SELECT("e.WarehouseId");

        FROM("component as a " +
                "RIGHT JOIN component_parameter as b " +
                "on a.ComponentId = b.ComponentId " +
                "RIGHT JOIN component_parameter_detail as c " +
                "on b.ComponentParameterId = c.ComponentParameterId " +
                "RIGHT JOIN ware_parameter_detail as d " +
                "on c.ComponentParameterDetailId = d.ComponentParameterDetailId " +
                "RIGHT JOIN stock as e " +
                "on e.WareId = d.WareId " +
                "left JOIN warehouse as f " +
                "on e.WarehouseId = f.WarehouseId " +
                "left join ware as g " +
                "on g.WareId = e.WareId");

        ApplyWareWhere(false, (Integer) parameter.get("componentid"), (Integer) parameter.get("componentparameterdetailid"),(Integer)  parameter.get("warehouseid"), (Ware) parameter.get("ware"));

        return SQL();
    }

    public String getFuzzyWareStock(Map<String, Object> parameter) {
        BEGIN();

        SELECT_DISTINCT("StockId");
        SELECT("WareName");
        SELECT("e.WareId");
        SELECT("ComponentName");
        SELECT("Barcode");
        SELECT("StockNum");
        SELECT("WarehouseName");
        SELECT("e.WarehouseId");

        FROM("component as a " +
                "RIGHT JOIN component_parameter as b " +
                "on a.ComponentId = b.ComponentId " +
                "RIGHT JOIN component_parameter_detail as c " +
                "on b.ComponentParameterId = c.ComponentParameterId " +
                "RIGHT JOIN ware_parameter_detail as d " +
                "on c.ComponentParameterDetailId = d.ComponentParameterDetailId " +
                "RIGHT JOIN stock as e " +
                "on e.WareId = d.WareId " +
                "left JOIN warehouse as f " +
                "on e.WarehouseId = f.WarehouseId " +
                "left join ware as g " +
                "on g.WareId = e.WareId");

        ApplyWareWhere(true, (Integer) parameter.get("componentid"), (Integer) parameter.get("componentparameterdetailid"),(Integer)  parameter.get("warehouseid"), (Ware) parameter.get("ware"));

        return SQL();
    }

    protected void ApplyWareWhere(boolean isFuzzy, Integer componentid, Integer componentparameterdetailid,  Integer warehouseid, Ware ware) {
        String andString = " AND ";

        StringBuffer sb = new StringBuffer();
        boolean firstWhere = true;

        if (componentid != null) {
            if (firstWhere) {
                firstWhere = !firstWhere;
            } else {
                sb.append(andString);
            }

            sb.append(" a.Componentid = #{componentid ,jdbcType=INTEGER}");
        }

        if (componentparameterdetailid != null) {
            if (firstWhere) {
                firstWhere = !firstWhere;
            } else {
                sb.append(andString);
            }

            sb.append(" d.Componentparameterdetailid = #{componentparameterdetailid ,jdbcType=INTEGER}");
        }

        if (warehouseid != null) {
            if (firstWhere) {
                firstWhere = !firstWhere;
            } else {
                sb.append(andString);
            }

            sb.append("e.Warehouseid = #{warehouseid,jdbcType=INTEGER}");
        }

        if (!StringUtils.isEmpty(ware.getBarcode())) {
            if (firstWhere) {
                firstWhere = !firstWhere;
            } else {
                sb.append(andString);
            }

            if (isFuzzy) {
                sb.append("LOCATE( #{ware.barcode,jdbcType=VARCHAR},Barcode)");
            } else {
                sb.append("Barcode = #{ware.barcode,jdbcType=VARCHAR}");
            }
        }

        if (!StringUtils.isEmpty(ware.getWarename())) {
            if (firstWhere) {
                firstWhere = !firstWhere;
            } else {
                sb.append(andString);
            }

            if (isFuzzy) {
                sb.append("LOCATE( #{ware.warename,jdbcType=VARCHAR},WareName)");
            } else {
                sb.append("WareName = #{ware.warename,jdbcType=VARCHAR}");
            }
        }

        if (!StringUtils.isEmpty(ware.getWaredescribe())) {
            if (firstWhere) {
                firstWhere = !firstWhere;
            } else {
                sb.append(andString);
            }

            if (isFuzzy) {
                sb.append("LOCATE( #{ware.waredescribe,jdbcType=VARCHAR},WareDescribe)");
            } else {
                sb.append("WareDescribe = #{ware.waredescribe,jdbcType=VARCHAR}");
            }
        }

        if (sb.length() > 0) {
            WHERE(sb.toString());
        }
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table stock
     *
     * @mbggenerated
     */
    protected void applyWhere(StockExample example, boolean includeExamplePhrase) {
        if (example == null) {
            return;
        }
        
        String parmPhrase1;
        String parmPhrase1_th;
        String parmPhrase2;
        String parmPhrase2_th;
        String parmPhrase3;
        String parmPhrase3_th;
        if (includeExamplePhrase) {
            parmPhrase1 = "%s #{example.oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{example.oredCriteria[%d].allCriteria[%d].value} and #{example.oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{example.oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{example.oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{example.oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        } else {
            parmPhrase1 = "%s #{oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{oredCriteria[%d].allCriteria[%d].value} and #{oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        }
        
        StringBuilder sb = new StringBuilder();
        List<Criteria> oredCriteria = example.getOredCriteria();
        boolean firstCriteria = true;
        for (int i = 0; i < oredCriteria.size(); i++) {
            Criteria criteria = oredCriteria.get(i);
            if (criteria.isValid()) {
                if (firstCriteria) {
                    firstCriteria = false;
                } else {
                    sb.append(" or ");
                }
                
                sb.append('(');
                List<Criterion> criterions = criteria.getAllCriteria();
                boolean firstCriterion = true;
                for (int j = 0; j < criterions.size(); j++) {
                    Criterion criterion = criterions.get(j);
                    if (firstCriterion) {
                        firstCriterion = false;
                    } else {
                        sb.append(" and ");
                    }
                    
                    if (criterion.isNoValue()) {
                        sb.append(criterion.getCondition());
                    } else if (criterion.isSingleValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase1, criterion.getCondition(), i, j));
                        } else {
                            sb.append(String.format(parmPhrase1_th, criterion.getCondition(), i, j,criterion.getTypeHandler()));
                        }
                    } else if (criterion.isBetweenValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase2, criterion.getCondition(), i, j, i, j));
                        } else {
                            sb.append(String.format(parmPhrase2_th, criterion.getCondition(), i, j, criterion.getTypeHandler(), i, j, criterion.getTypeHandler()));
                        }
                    } else if (criterion.isListValue()) {
                        sb.append(criterion.getCondition());
                        sb.append(" (");
                        List<?> listItems = (List<?>) criterion.getValue();
                        boolean comma = false;
                        for (int k = 0; k < listItems.size(); k++) {
                            if (comma) {
                                sb.append(", ");
                            } else {
                                comma = true;
                            }
                            if (criterion.getTypeHandler() == null) {
                                sb.append(String.format(parmPhrase3, i, j, k));
                            } else {
                                sb.append(String.format(parmPhrase3_th, i, j, k, criterion.getTypeHandler()));
                            }
                        }
                        sb.append(')');
                    }
                }
                sb.append(')');
            }
        }
        
        if (sb.length() > 0) {
            WHERE(sb.toString());
        }
    }
}